package com.threeti.mecool.core.infrastructure.persistence.plan;

import com.threeti.mecool.core.domain.model.plan.PlanJobResult;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.Date;

public class PlanJobResultSpecs {
    public static Specification<PlanJobResult> finishedJob() {
        return new Specification<PlanJobResult>() {
            @Override
            public Predicate toPredicate(Root<PlanJobResult> planJobResultRoot, CriteriaQuery<?> query, CriteriaBuilder cb) {
                return cb.equal(planJobResultRoot.<String>get("jobStatus"), PlanJobResult.JOB_STATUS_FINISHED);
            }
        };
    }

    public static Specification<PlanJobResult> finishAtGreaterThanOrEqualTo(final Date finishedAt) {
        return new Specification<PlanJobResult>() {
            @Override
            public Predicate toPredicate(Root<PlanJobResult> planJobResultRoot, CriteriaQuery<?> query, CriteriaBuilder cb) {
                return cb.greaterThanOrEqualTo(planJobResultRoot.<Date>get("finishedAt"), finishedAt);
            }
        };
    }

    public static Specification<PlanJobResult> finishAtLessThat(final Date finishedAt) {
        return new Specification<PlanJobResult>() {
            @Override
            public Predicate toPredicate(Root<PlanJobResult> planJobResultRoot, CriteriaQuery<?> query, CriteriaBuilder cb) {
                return cb.lessThan(planJobResultRoot.<Date>get("finishedAt"), finishedAt);
            }
        };
    }
}
